<?php
namespace common\component\tool;

use Yii;
use common\component\tool\HttpClient;

/*
会员类
*/
class Member
{

    public $httpClient;
	public $baseUrl;
    public $params;

    public function __construct($baseUrl)
    {
        $this->baseUrl = $baseUrl;
        $this->httpClient = new HttpClient($this->baseUrl);
        $this->params = Yii::$app->params;
    }


    /*微信公众授权注册*/
    public function authByWechatMiniprogram($data, $appInfoId)
    {
        // P($data);
        $data['regip'] = Yii::$app->request->userIP;
        $data['regclient'] = $_SERVER['HTTP_USER_AGENT'];
        $this->httpClient->url = '/member/reg/reg-by-wechat';
        $data = [
            'Member' => $data,
            'appInfoId' => $appInfoId
        ];
        // P($data);
        $this->httpClient->data = $data;
        if($res = $this->httpClient->post()){
            // P($res);
            return $res;
        }
    }

    /*根据 openid 查询用户信息，并存入session，主要用户小程序过期重复授权的问题*/
    public function getinfoByOpenid($data, $appInfoId)
    {
        $data['secretkey'] = $this->params['secretkey']['ac'];
        $data['regip'] = Yii::$app->request->userIP;
        $data['regclient'] = $_SERVER['HTTP_USER_AGENT'];
        $this->httpClient->url = '/member/login/getinfo-by-openid';
        $data = [
            'Member' => $data,
            'appInfoId' => $appInfoId
        ];
        // P($data);
        $this->httpClient->data = $data;
        if($res = $this->httpClient->post()){
            // P($res);
            return $res;
        }
    }

    /*
    注册
    $data 		注册数据
    $appInfoId 
    */
    public function register($data, $appInfoId)
    {
    	// P($data);
    	$data['regip'] = Yii::$app->request->userIP;
        $data['regclient'] = $_SERVER['HTTP_USER_AGENT'];
        $this->httpClient->url = '/member/reg/reg';
        $data = [
            'Member' => $data,
            'appInfoId' => $appInfoId
        ];
        // P($data);
        $this->httpClient->data = $data;
        if($res = $this->httpClient->post()){
            // P($res);
            return $res;
        }

    }

    /*登录*/
    public function login($data, $appInfoId)
    {
        $data['lastloginip'] = Yii::$app->request->userIP;
        $this->httpClient->url = '/member/login/login';
        $data = [
            'Member' => $data,
            'appInfoId' => $appInfoId
        ];
        // P($data);
        $this->httpClient->data = $data;
        if($res = $this->httpClient->post()){
            // P($res);
            return $res;
        }
    }

    /*忘记密码*/
    public function forget($data, $appInfoId)
    {
        $this->httpClient->url = '/member/login/forget';
        $data = [
            'Member' => $data,
            'appInfoId' => $appInfoId
        ];
        // P($data);
        $this->httpClient->data = $data;
        if($res = $this->httpClient->post()){
            // P($res);
            return $res;
        }
    }

    /*
    获取会员信息
    返回值     数组
    */
    public function getInfo($member_id, $appInfoId)
    {
        $this->httpClient->url = '/member/set/get-info';
        $data = [
            'member_id' => $member_id,
            'appInfoId' => $appInfoId
        ];
        // P($data);
        $this->httpClient->data = $data;
        if($res = $this->httpClient->post()){
            // P($res);
            return $res;
        }
    }

    /*修改会员信息*/
    public function modMemberInfo($data, $appInfoId)
    {
        $this->httpClient->url = '/member/set/mod-info';
        $data = [
            'Member' => $data,
            'appInfoId' => $appInfoId
        ];
        // P($data);
        $this->httpClient->data = $data;
        if($res = $this->httpClient->post()){
            // P($res);
            return $res;
        }
    }


    /*修改密码 单独修改*/
    public function modMemberPassword($data, $appInfoId)
    {
        $this->httpClient->url = '/member/set/mod-password';
        $data = [
            'Member' => $data,
            'appInfoId' => $appInfoId
        ];
        // P($data);
        $this->httpClient->data = $data;
        if($res = $this->httpClient->post()){
            // P($res);
            return $res;
        }
    }

}